package com.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.entity.Food;
import com.soft.service.FoodService;
import com.soft.service.OrderlistService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/food")
@CrossOrigin(origins = "*")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @Autowired
    private OrderlistService orderlistService;

 // 分页查询菜品
    @GetMapping("/menu")
    public Page<Food> menu(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String category
    ) {
        return foodService.listByCategoryPage(page, size, category);
    }
    //  不分页查询所有菜品
    @GetMapping("/menuAll")
    public List<Food> menuAll(@RequestParam(required = false) String category) {
        return foodService.listByCategory(category);
    }

    @GetMapping("/categories")
    public List<String> categories() {
        return foodService.listCategories();
    }

    @GetMapping("/checkAndReduceStock")
    public String checkAndReduceStock(
            @RequestParam List<Long> foodId,
            @RequestParam List<Long> foodCount
    ) {
        List<Food> orderItems = new ArrayList<>();
        for (int i = 0; i < foodId.size(); i++) {
            Food f = new Food();
            f.setFoodId(foodId.get(i));
            f.setFoodCount(foodCount.get(i));
            orderItems.add(f);
        }
        return foodService.checkAndReduceStock(orderItems);
    }

    @GetMapping("/monthlySales")
    public Map<String, Long> monthlySales() {
        return orderlistService.getMonthlyGoodsSales();
    }

    @PostMapping("/add")
    public String addFood(@RequestBody Food food) {
        int result = foodService.addFood(food);
        return result > 0 ? "success" : "fail";
    }

    @PostMapping("/update")
    public String updateFood(@RequestBody Food food) {
        int result = foodService.updateFood(food);
        return result > 0 ? "success" : "fail";
    }

    @GetMapping("/deleteById")
    public String deleteFood(@RequestParam Long foodId) {
        int result = foodService.deleteFood(foodId);
        return result > 0 ? "success" : "fail";
    }
    @PostMapping("/status")  // 与前端请求的接口地址保持一致
    public int updateFoodStatus(
            @RequestParam Long foodId,
            @RequestParam String foodStatus) {
        // 验证参数
        if (foodId == null || foodStatus == null) {
            return 0;
        }
        // 调用服务层更新状态
        int success = foodService.updateFoodStatus(foodId, foodStatus);
        return success;
    }
    @GetMapping("/checkCart")
    public Map<Long, String> checkCart(@RequestParam List<Long> foodIds) {
        // 调用严格版检查
        return foodService.checkCart(foodIds);	
    }



}
